import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
import userRouter from './modules/user'

// 动态路由的创建，代表的是一个数组里面每一个路由对象
export const asyncRoutes = [
  approvalsRouter,
  departmentsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter

]
// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true // 控制该路由在菜单中是否展示
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout, // 属于一级路由，是布局的主页面
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  // 上传文件的路由配置
  {
    path: '/import',
    component: Layout,
    hidden: true, // 隐藏在左侧菜单中
    children: [{
      path: '', // 二级路由path什么都不写 表示二级默认路由
      component: () => import('@/views/import')
    }]
  },
  userRouter

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]
// const createRouter = () => new Router({
//   // mode: 'history', // require service support
//   scrollBehavior: () => ({ y: 0 }),
//   routes: constantRoutes
// })
const createRouter = () => new Router({
  mode: 'history', // require service support
  // base: 'heima',
  scrollBehavior: () => ({ y: 0 }), // 管理页面的滚动行为
  // routes: [...constantRoutes, ...asyncRoutes]// 临时合并所有的路由
  routes: [...constantRoutes] // 临时合并所有的路由
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 这是重置路由的方法
export function resetRouter() {
  const newRouter = createRouter() // 重新调用了创建路由实例的方法
  router.matcher = newRouter.matcher // reset router 用新路由的matcher方法替换旧路由的matcher方法达到重置的目的
}

export default router
